const app = getApp();
const regeneratorRuntime = global.regeneratorRuntime = require('../../libs/regenerator');
const co = require("../../libs/co");
const http = require('../../libs/httpRequest');
const appConfig = require('../../appConfig');

let CartService = function () {
  let _addCartProduct = function *(productId,skuId,skuAmount,count) {
    let ret = yield http.request({
        url: `${appConfig.api.baseUrl}/cart/add`,
        data: {productId:productId,skuId:skuId,amount:skuAmount,count:count},
        method: 'POST',
        success: (res) => {
            console.log(res.data);
        },
        fail: (err) => {
            console.log(err);
        }
    });
    return ret.apiStatus.statusCode == 0 ? true : false;
  }

  let _getCartList = function *(pageIndex,pageSize) {
    let ret = yield http.request({
        url: `${appConfig.api.baseUrl}/cart/list`,
        data: {pageIndex:pageIndex,pageSize:pageSize},
        method: 'POST',
        success: (res) => {
            console.log(res.data);
        },
        fail: (err) => {
            console.log(err);
        }
    });
    return ret;
  }

  let _updateCount = function *(productId,type,skuId) {
    let ret = yield http.request({
        url: `${appConfig.api.baseUrl}/cart/edit`,
        data: {productId:productId,'type':type,skuId:skuId},
        method: 'POST',
        success: (res) => {
            console.log(res.data);
        },
        fail: (err) => {
            console.log(err);
        }
    });
    return ret;
  }

  let _deleteCart = function *(productId,skuId) {
    let ret = yield http.request({
        url: `${appConfig.api.baseUrl}/cart/del`,
        data: {productId:productId,skuId:skuId},
        method: 'POST',
        success: (res) => {
            console.log(res.data);
        },
        fail: (err) => {
            console.log(err);
        }
    });
    return ret;
  }

  let _getSelectCart = function *(skuIds){
    let ret = yield http.request({
        url: `${appConfig.api.baseUrl}/cart/selectList`,
        data: {skuIds:skuIds},
        method: 'POST',
        success: (res) => {
            console.log(res.data);
        },
        fail: (err) => {
            console.log(err);
        }
    });
    return ret;
  }
  return {
    addCartProduct : _addCartProduct,
    getCartList : _getCartList,
    updateCount : _updateCount,
    deleteCart : _deleteCart,
    getSelectCart : _getSelectCart
  }
}();

module.exports = CartService;
